package com.xhwl.centiotdata.action.pedestrian.impl;

import com.huaweicloud.sdk.core.exception.ClientRequestException;
import com.huaweicloud.sdk.iotda.v5.IoTDAClient;
import com.huaweicloud.sdk.iotda.v5.model.CreateCommandRequest;
import com.huaweicloud.sdk.iotda.v5.model.CreateCommandResponse;
import com.huaweicloud.sdk.iotda.v5.model.DeviceCommandRequest;
import com.xhwl.centiotdata.action.pedestrian.ILightAction;
import com.xhwl.centiotdata.handler.light.LightIntegrationFactory;
import com.xhwl.centiotdata.handler.light.LightIntegrationHandler;
import com.xhwl.centiotdata.pojo.constant.HuaWeiIoTDAConstant;
import com.xhwl.centiotdata.pojo.dto.huawei.IOTConfigDTO;
import com.xhwl.centiotdata.pojo.dto.pedestrian.PedestrianIntegrationConfigDTO;
import com.xhwl.centiotdata.service.cent.IDeviceCentService;
import com.xhwl.centiotdata.service.integrateconfig.IntegrationConfig;
import com.xhwl.common.dto.cent.device.DeviceDTO;
import com.xhwl.common.dto.cent.iotdata.light.LightActionDTO;
import com.xhwl.common.pojo.ResultJson;
import com.xhwl.common.pojo.cent.device.DeviceProjSupplierCategory;
import com.xhwl.common.query.cent.device.DevicePageQuery;
import com.xhwl.common.query.cent.device.DeviceQuery;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author: yanyixiong
 * @Description:
 * @Date: create in 17:53 2022/1/11
 */
@Slf4j
@Service
public class LightActionImpl implements ILightAction {
    @Autowired
    private IDeviceCentService deviceCentService;

    @Override
    public boolean switchLight(LightActionDTO lightActionDTO) {
        DevicePageQuery deviceQuery = new DevicePageQuery();
        deviceQuery.setIotDeviceId(lightActionDTO.getLoopCode());
        List<DeviceDTO> deviceDTOList = deviceCentService.listDevice(null, deviceQuery);
        if (CollectionUtils.isEmpty(deviceDTOList)) {
            log.error("未找到设备！");
            return false;
        }
        DeviceDTO deviceDTO = deviceDTOList.get(0);
        Integer productId = deviceDTO.getProductId();
        LightIntegrationHandler handler = LightIntegrationFactory.getInvokeStrategy(productId);
        if(handler == null){
            log.error("未找到处理方式");
            return false;
        }
        Integer projectId = deviceDTO.getProjectId();
        List<DeviceProjSupplierCategory> deviceProjSupplierCategoryList = IntegrationConfig.integrationConfigList.stream().filter(bean -> {
            return bean.getProductId() == 1 && bean.getProjectId().equals(projectId);
        }).collect(Collectors.toList());
        if(deviceProjSupplierCategoryList.isEmpty()){
            log.error("未集成配置");
            return false;
        }
        IOTConfigDTO iotConfigDTO = deviceProjSupplierCategoryList.get(0).getProperties().toJavaObject(IOTConfigDTO.class);
        return handler.switchLight(iotConfigDTO, lightActionDTO);
    }

    @Override
    public List<LightActionDTO> batchSwitchLight(List<LightActionDTO> lightActionDTOList) {
        if(lightActionDTOList.isEmpty()){
            log.error("设备列表为空！");
            return lightActionDTOList;
        }
        DevicePageQuery deviceQuery = new DevicePageQuery();
        deviceQuery.setIotDeviceId(lightActionDTOList.get(0).getLoopCode());
        List<DeviceDTO> deviceDTOList = deviceCentService.listDevice(null, deviceQuery);
        if (CollectionUtils.isEmpty(deviceDTOList)) {
            log.error("未找到设备！");
            return lightActionDTOList;
        }
        DeviceDTO deviceDTO = deviceDTOList.get(0);
        Integer productId = deviceDTO.getProductId();
        LightIntegrationHandler handler = LightIntegrationFactory.getInvokeStrategy(productId);
        if(handler == null){
            log.error("未找到处理方式");
            return lightActionDTOList;
        }
        Integer projectId = deviceDTO.getProjectId();
        List<DeviceProjSupplierCategory> deviceProjSupplierCategoryList = IntegrationConfig.integrationConfigList.stream().filter(bean -> {
            return bean.getProductId() == 1 && bean.getProjectId().equals(projectId);
        }).collect(Collectors.toList());
        if(deviceProjSupplierCategoryList.isEmpty()){
            log.error("未集成配置");
            return lightActionDTOList;
        }
        IOTConfigDTO iotConfigDTO = deviceProjSupplierCategoryList.get(0).getProperties().toJavaObject(IOTConfigDTO.class);

        for (LightActionDTO lightActionDTO : lightActionDTOList) {
            lightActionDTO.setResult(handler.switchLight(iotConfigDTO, lightActionDTO));
        }
        return lightActionDTOList;
    }

}
